package com.pateo.bjev.contacts.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

import com.pateo.bjev.contacts.BjevContactsManager;
import com.pateo.bjev.contacts.IBtStateListener;
import com.pateo.bjev.contacts.IPbapStateListener;
import com.pateo.bjev.contacts.utils.Log;
import com.pateo.bluetooth.logic.bean.ContactData;
import com.pateo.bluetooth.logic.bean.ContactLog;
import com.pateo.bluetooth.logic.contact.manager.ContactsManager;

import java.util.List;

public class ContactService extends Service {

    ContactsHandler contactsHandler;

    @Override
    public void onCreate() {
        super.onCreate();
        contactsHandler = new ContactsHandler(this);
        Log.d(ContactService.class,"oncreate");
    }

    @Override
    public IBinder onBind(Intent intent) {
        return manager.asBinder();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(contactsHandler!=null){
            contactsHandler.unRegisterEvent();
        }
    }

    BjevContactsManager manager = new BjevContactsManager.Stub(){

        @Override
        public void registerBtStateListener(IBtStateListener listener) throws RemoteException {
            if(contactsHandler!=null){
                contactsHandler.registBtStateListener(listener);
            }
        }

        @Override
        public void unregisterBtStateListener(IBtStateListener listener) throws RemoteException {
            if(contactsHandler!=null){
                contactsHandler.unregistBtStateListener(listener);
            }
        }

        @Override
        public void registerPbapSyncListener(IPbapStateListener listener) throws RemoteException {
            if(contactsHandler!=null){
                contactsHandler.registPbapStateListener(listener);
            }
        }

        @Override
        public void unregisterPbapSyncListener(IPbapStateListener listener) throws RemoteException {
            if(contactsHandler!=null){
                contactsHandler.unregistPbapStateListener(listener);
            }
        }

        @Override
        public int getBtState() throws RemoteException {
            return contactsHandler.getState();
        }

        @Override
        public int getPbapState() throws RemoteException {
            return contactsHandler.getPbapState();
        }

        @Override
        public List<ContactData> getContacts() throws RemoteException {
            return contactsHandler.contacts;
        }

        @Override
        public List<ContactLog> getContactLogs() throws RemoteException {
            return contactsHandler.contactLogs;
        }
    };
}
